<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/laypage.css">
    <script src="js/vue/vue.min.js"></script>
    <script src="js/jquery/jquery.min.js"></script>
    <script src="js/laypage/laypage.js" charset="utf-8"></script>
    <script src="js/layer/layer.js" charset="utf-8"></script>
</head>
<body>
<div id="app" class="container">

    <form class="form-inline bg-danger" role="form">
        查询条件：
        <div class="form-group">
            <label class="sr-only" for="name">用户名称</label>
            <input type="text" class="form-control" id="name" placeholder="用户名称">
        </div>
        <div class="form-group">
            <label class="sr-only" for="age">用户年龄</label>
            <input type="text" class="form-control" id="age" placeholder="用户年龄">
        </div>
        <button type="button" id="findUser" class="btn btn-success">查询用户</button>
        <button type="button" id="addPersonBtn" class="btn btn-danger">增加用户</button>
    </form>

    <div class="table-responsive">
        <table class="table">
            <thead>
            <tr class="success">
                <td>序号</td>
                <td>用户</td>
                <td>年龄</td>
                <td>密码</td>
                <td>邮箱</td>
                <td>操作</td>
            </tr>
            </thead>
            <tbody>
            <tr class="active" v-for="(item,index) in result">
                <td>{{index+1}}</td>
                <td>{{item.name}}</td>
                <td>{{item.age}}</td>
                <td>{{item.password}}</td>
                <td></td>
                <td><a href="#" @click="updatePerson(item.id)">编辑</a>&nbsp; <a href="#" @click="delPerson(item.id)">删除</a></td>
            </tr>
            </tbody>
        </table>
        <div id="pagenav"></div>
    </div>
</div>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            result: []
        }
    })

    var getUserPageList = function (curr) {
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: 'getcond',
            data: {
                pageSize: 3,
                pageNum: curr || 1,
                name: $("#name").val(),
                age: $("#age").val()
            },
            success: function (msg) {
                app.result = msg.result;
                laypage({
                    cont: 'pagenav',
                    skin: '#625',
                    pages: msg.totalPage,
                    first: '首页',
                    last: '最后一页',
                    curr: curr || 1,
                    jump: function (obj, first) {
                        if (!first) {
                            getUserPageList(obj.curr);
                        }
                    }
                });
            }
        });
    };

    getUserPageList();

    $("#findUser").click(function () {
        getUserPageList();
    });


    //添加
    var addPerson  = function () {
        layer.open({
            type:2,
            title:"添加用户",
            //窗口是否固定
            fixed:false,
            //可伸缩大小
            maxmin:true,
            //是否点击遮罩关闭
            shadeClose:true,
            //宽高
            area:['1100px','600px'],
            //内容
            content:'add_person.html',
            end:function () {
                getUserPageList();
            }
        })
    };


    $("#addPersonBtn").click(function () {
        addPerson();
    });
    
    //删除
    var delPerson = function (id) {
        layer.confirm(
            '确定要删除吗',
            {btn:['是','否']},
            function () {
                $.ajax({
                    type:'post',
                    data:{id:id},
                    url:'person_del',
                    dataType:'json',
                    success:function () {
                        getUserPageList();
                    }
                })
            }
        )
    };

    //编辑
    var updatePerson = function (id) {
        layer.open({
            type:2,
            title:"编辑用户",
            //窗口是否固定
            fixed:false,
            //可伸缩大小
            maxmin:true,
            //是否点击遮罩关闭
            shadeClose:true,
            //宽高
            area:['1100px','600px'],
            //内容
            content:'person_getById?id='+id,
            end:function () {
                getUserPageList();
            }
        })
    }


</script>

</body>
</html>